try:
    from adh_ai_agent.calc_24_points_by_cline_raw import calc_24_points
except ImportError:
    from calc_24_points_by_cline_raw import calc_24_points

def remove_redundant_parentheses(expression):
    """
    Remove one pair of redundant parentheses from the expression if they don't affect the calculation result.
    
    Args:
        expression (str): The mathematical expression to simplify
        
    Returns:
        str: The simplified expression if parentheses were redundant, otherwise the original expression
    """
    # If no parentheses, return the expression as is
    if '(' not in expression and ')' not in expression:
        return expression
    
    # Find all pairs of parentheses
    pairs = []
    stack = []
    
    for i, char in enumerate(expression):
        if char == '(':
            stack.append(i)
        elif char == ')':
            if stack:
                start = stack.pop()
                pairs.append((start, i))
    
    # Check each pair of parentheses
    for start, end in pairs:
        # Remove this pair of parentheses
        new_expr = expression[:start] + expression[start+1:end] + expression[end+1:]
        
        try:
            # Check if the results are the same after removing parentheses
            original_result = eval(expression)
            new_result = eval(new_expr)
            
            # If results are essentially the same, return the simplified expression
            if abs(original_result - new_result) < 1e-10:
                return new_expr
        except:
            continue
    
    # If no simplification possible, return the original expression
    return expression

def calc_all_expressions(numbers):
    """
    Calculate all valid expressions that equal 24 using the given numbers,
    and remove redundant parentheses from each expression.
    
    Args:
        numbers (list): List of four natural numbers (1-13)
        
    Returns:
        list: List of simplified valid expressions that evaluate to 24
    """
    # Get the original expressions
    raw_expressions = calc_24_points(numbers)
    
    # Remove redundant parentheses from each expression
    simplified_expressions = [remove_redundant_parentheses(expr) for expr in raw_expressions]
    
    # Remove duplicates while preserving order
    unique_results = []
    for expr in simplified_expressions:
        if expr not in unique_results:
            unique_results.append(expr)
    
    return unique_results

# Test code
if __name__ == "__main__":
    test_numbers = [7, 11, 3, 10]
    results = calc_all_expressions(test_numbers)
    
    print(f"Found {len(results)} valid expressions for {test_numbers}:")
    for expr in results:
        try:
            result = eval(expr)
            print(f"{expr} = {result}")
        except:
            print(f"{expr} = Error")

    # Verify all results are correct
    print("\nVerifying results:")
    for expr in results:
        try:
            assert abs(eval(expr) - 24) < 1e-10, f"Expression {expr} does not equal 24"
            print(f"✓ {expr}")
        except Exception as e:
            print(f"✗ {expr} - {str(e)}")
